<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="140px" label-position="left">
    <el-form-item :label="$t('user.oldpwd')" prop="oldPassword">
      <el-input v-model="user.oldPassword" :placeholder="$t('plc.oldpwd')" type="password" />
    </el-form-item>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item :label="$t('user.newpwd')" prop="newPassword">
          <el-input v-model="user.newPassword" :placeholder="$t('plc.newpwd')" type="password" show-password/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item :label="$t('user.cfmpwd')" prop="confirmPassword">
          <el-input v-model="user.confirmPassword" :placeholder="$t('plc.cfmpwd')" type="password" show-password/>
        </el-form-item></el-col>
    </el-row>
    <div class="col-12 mb-4 pwd-tip">
      <h6>{{$t('desc.pwdmust')}}</h6>
      <ul class="ps-4 mb-0">
        <li class="mb-4">{{$t('desc.pwdmustd1')}}</li>
        <li class="mb-4">{{$t('desc.pwdmustd2')}}</li>
        <li>{{$t('desc.pwdmustd3')}}</li>
      </ul>
    </div>
    
    <el-button type="primary" size="mini" @click="submit">{{$t('btn.savechs')}}</el-button>
      <el-button class="btn-label-secondary" size="mini" @click="close">{{$t('btn.cancel')}}</el-button>
  </el-form>
</template>

<script>
import { updateUserPwd } from "@/api/system/user";

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      test: "1test",
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" }
        ],
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(
            response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
              }
            }
          );
        }
      });
    },
    close() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: "/index" });
    }
  }
};
</script>
